package model

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"homework12/config"
)

var err error
var DB *gorm.DB

func Init() {
	str := config.Config.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		str.User,
		str.Password,
		str.Host,
		str.Port,
		str.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	fmt.Println("数据量链接成功")
	err = DB.AutoMigrate(&Orders{})
	if err != nil {
		panic(err)
	}
	fmt.Println("数据表迁移成功")
}
